part of '../service.dart';

const _kAccessTokenClaimUserIdKey = 'user_id';
const _kAccessTokenClaimUserRoleKey = 'user_role';

class _AccessTokenClaimDto extends Equatable {
  const _AccessTokenClaimDto._internal({
    required this.userId,
    required this.userRoleDto,
  });

  final String userId;
  final _UserRoleDto userRoleDto;

  @override
  List<Object?> get props {
    return [
      userId,
      userRoleDto,
    ];
  }

  factory _AccessTokenClaimDto.fromJson(Map<String, dynamic> json) {
    final userRoleString = json[_kAccessTokenClaimUserRoleKey] as String;
    final userRoleDto = _UserRoleDto.fromString(userRoleString);

    return _AccessTokenClaimDto._internal(
      userId: json[_kAccessTokenClaimUserIdKey],
      userRoleDto: userRoleDto,
    );
  }

  factory _AccessTokenClaimDto.fromEntity(AccessTokenClaim accessTokenClaim) {
    final userRoleDto = _UserRoleDto.fromEntity(accessTokenClaim.userRole);

    return _AccessTokenClaimDto._internal(
      userId: accessTokenClaim.userId,
      userRoleDto: userRoleDto,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kAccessTokenClaimUserIdKey: userId,
      _kAccessTokenClaimUserRoleKey: userRoleDto.toString(),
    };
  }

  AccessTokenClaim toEntity() {
    return AccessTokenClaim(
      userId: userId,
      userRole: userRoleDto.toEntity(),
    );
  }
}
